// Copyright 2007-2008 Helsinki Institute for Information Technology
// and the authors.
//
// Authors: Harri Hämäläinen <hhamalai@iki.fi>

// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function hasC(id, name)
{
    var class_names = getC(id);
    for ( var i = 0; i < class_names.length; ++i) {
        if (class_names[i] == name) {
            return true;
        }
    }
    return false;
}

function putC(id, v)
{
    var field = document.getElementById( id );
    field.setAttribute( 'class', v );
}

function getC(id)
{
    var field = document.getElementById( id );
    var v = field.getAttribute( 'class' );
    if (Object.isString(v)) {
        return v.split(/\s+/);
    } else {
        return [];
    }
}

function addC(id, name)
{
    var class_names = getC(id);
    for ( var i = 0; i < class_names.length; ++i) {
        if (class_names[i] == name) {
            return;
        }
    }
    class_names.push(name);
    putC(id, class_names.join(' '));
}

function delC(id, name)
{
    var class_names = getC(id);
    var found = -1;
    for ( var i = 0; i < class_names.length; ++i) {
        if (class_names[i] == name) {
            found = i;
        }
    }
    if (found >= 0) {
        class_names.splice(found, 1);
        putC(id, class_names.join(' '));
    }
}

function putA(id, name, value)
{
  var field = document.getElementById( id );
  field.setAttribute(name,value);
}

function getA(id, name)
{
  var field = document.getElementById( id );
  return field.getAttribute(name);
}

function delA(id, name)
{
  var field = document.getElementById( id );
  field.removeAttribute(name);
}

function putV(id, value)
{
  var field = document.getElementById( id );
  field.value = value;
}

function getV(id)
{
  var field = document.getElementById( id );
  return $(id).value;
}

function putI(id, value)
{
  var field = document.getElementById( id );
  field.innerHTML = value;
}

function getI(id)
{
  var field = document.getElementById( id );
  alert(id);
  alert(field.innerHTML);
  return field.innerHTML;
}


function disableRights(right)
{
    HttpConnector.doDelete(right);
}

function enableRights(right)
{
    HttpConnector.doPut(right, '{}');
}

function escape(text)
{
    if (text instanceof Array) {
        for (var i=0; i<text.length; i += 1) {
            text[i] = escape(text[i]);
        }
    }
    else {
        text = text.replace(/</g,'&lt;');
        text = text.replace(/>/g,'&gt;');
    }
    return text;
}

function getDefaultProfile()
{
    var info = HttpConnector.doGet('/content/?parent=T').evalJSON(true);
    return info.parent;
}

function getRightedGroups(right)
{
    var result = HttpConnector.doGet('/content/rights/?output=json').evalJSON(true);
    var data = result.data;
    var rights = [];
    for (var i = 0; i < data.length; i += 1) {
        var k = data[i][0];
        var v = data[i][1];
        if (v == '{}') {
            var parts = k.split('/');
            var real_path = parts.slice(1).join('/');
            if (right == real_path) {
                rights.push(real_path);
            }
        }
    }
    return rights;
}

function getUTCDate()
{
    var localDate = new Date();
    var date = new Date(localDate.valueOf() +
        localDate.getTimezoneOffset()*1000*60);

    var ret = '';
    
    ret += date.getFullYear()+'-';
    var month = (date.getMonth()+1);
    
    ret += pad(month)+'-'+pad(date.getDate())+'T';
    ret += pad(date.getHours())+':'+ pad(date.getMinutes()) +':';
    ret += pad(date.getSeconds());
    ret += 'Z'
    return ret;
}

function getFriendlyDate(inputDate)
{
    var date = inputDate.match(/(\d+-\d+-\d+)/)
    if (date) {
    	date = date[1];
    	date += " " + inputDate.match(/(\d+:\d+:\d+)/)[1];
    }
    return date;
}

function getUTCFriendlyDate(inputDate)
{
    var localDate = new Date();
    var uniDate = parseUTCDate(inputDate);
    localDate = new Date(uniDate.valueOf() -
        localDate.getTimezoneOffset()*1000*60);
    var ret = '';
    ret += localDate.getFullYear()+'-';
    var month = (localDate.getMonth()+1);
    ret += pad(month)+'-'+pad(localDate.getDate())+'T';
    ret += pad(localDate.getHours())+':'
        + pad(localDate.getMinutes()) +':';
    ret += pad(localDate.getSeconds());
    return getFriendlyDate(ret);
}


function pad(s)
{
    var ret = ''+s;
    if (ret.length<2)
        ret='0'+ret;
    return ret;
}

function parseUTCDate(isodate)
{   
    var year = parseInt(isodate.substring(0,4), 10);        
    var month = parseInt(isodate.substring(5,7), 10);   
    var day = parseInt(isodate.substring(8,10), 10);
    
    var hour = parseInt(isodate.substring(11,13), 10);
    var minute = parseInt(isodate.substring(14,16), 10);
    var second =  parseInt(isodate.substring(17,19), 10);   
    var millisecond = parseInt(isodate.substring(20,23), 10); 

    var date = new Date();  
    
    date.setFullYear(year); 
    date.setMonth(month-1);
    date.setDate(day);  
    date.setHours(hour);

    date.setMinutes(minute);        
    date.setSeconds(second);
    date.setMilliseconds(millisecond);

    return date;
}


function randomHex(size)
{
  var ret = "0x"
  for (var i=0; i<size; i+=1) {
    var hex = (Math.random()*16 | 0);
    ret += hex.toString(16);
  }
  return ret;
}

